package mww.javabean.service;

import java.util.List;
import java.util.Map;

import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.dao.StudentCDao;
import mww.javabean.dao.StudentCDaoImpl;
import mww.javabean.entity.Score;
import mww.javabean.entity.StudentC;

public class StudentCServiceImpl implements StudentCService {
	StudentCDao studentCDao = new StudentCDaoImpl();
	
	@Override
	public void addStudentC(StudentC studentC) throws ServiceException {
		try {
			studentCDao.addStudentC(studentC);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
	}

	@Override
	public void delStudentCById(String studentNo, String teacherNo,String courseNo, Integer tcTeacherNo) throws ServiceException {
		try {
			studentCDao.delStudentCById(studentNo, teacherNo,courseNo,tcTeacherNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
	}
	
	@Override
	public void delStudentCByTeacherNo(String teacherNo) throws ServiceException {
		try {
			studentCDao.delStudentCByTno(teacherNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
	}

	@Override
	public void delStudentCByTnoCno(String teacherNo, String courseNo) throws ServiceException {
		try {
			studentCDao.delStudentCByTnoCno(teacherNo, courseNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
	}

	@Override
	public List findStudentCByStudentNo(String studentNo) throws ServiceException {
		List list = null;
		try {
			list = studentCDao.findStudentCByStudentNo(studentNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return list;
	}

	@Override
	public List findAllStudentById(String teacherNo, String courseNo, Integer tcTeacherNo) throws ServiceException {
		List list = null;
		try {
			list = studentCDao.findAllStudentById(teacherNo, courseNo, tcTeacherNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return list;
	}

	@Override
	public List findAllCourseByStudentNo(String studentNo) throws ServiceException {
		List list = null;
		try {
			list = studentCDao.findAllCourseByStudentNo(studentNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return list;
	}

	@Override
	public Score findMaxMinAvg(String teacherNo, String courseNo, Integer tcTeacherNo) throws ServiceException {
		Score score = null;
		try {
			score = studentCDao.findMaxMinAvg(teacherNo, courseNo, tcTeacherNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return score;
	}

	@Override
	public boolean checkConflict(String studentNo, String tcTime) throws ServiceException {
		boolean flag = true;
		try {
			flag = studentCDao.checkConflict(studentNo, tcTime);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return flag;
	}


	@Override
	public void updateScore(String studentNo, String teacherNo,String courseNo,  Integer tcTeacherNo, float usualScore, float lastScore,Integer status)
			throws ServiceException {
		float terminalScore = (float) (usualScore*0.3 +lastScore*0.7);
		float scoreGPA = calGPA(terminalScore);
		try {
			StudentC studentC = new StudentC();
			studentC.setStudentNo(studentNo);
			studentC.setTeacherNo(teacherNo);
			studentC.setCourseNo(courseNo);
			studentC.setTcTeacherNo(tcTeacherNo);
			studentC.setUsualScore(usualScore);
			studentC.setLastScore(lastScore);
			studentC.setTerminalScore(terminalScore);
			studentC.setScoreGPA(scoreGPA);
			studentC.setStatus(status);
			studentCDao.updateScore(studentC);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
	}
	
	
	public float calGPA(Float terminalScore) {
		float GPA = 0;
		if (terminalScore >= 90 && terminalScore <= 100) {
			GPA = 4.0f;
		} else if (terminalScore >= 85 && terminalScore < 90) {
			GPA = 3.7f;
		} else if (terminalScore >= 82 && terminalScore < 85) {
			GPA = 3.3f;
		} else if (terminalScore >= 78 && terminalScore < 82) {
			GPA = 3.0f;
		} else if (terminalScore >= 75 && terminalScore < 78) {
			GPA = 2.7f;
		} else if (terminalScore >= 72 && terminalScore < 75) {
			GPA = 2.3f;
		} else if (terminalScore >= 68 && terminalScore < 72) {
			GPA = 2.0f;
		} else if (terminalScore >= 66 && terminalScore < 68) {
			GPA = 1.7f;
		} else if (terminalScore >= 64 && terminalScore < 66) {
			GPA = 1.3f;
		} else if (terminalScore >= 60 && terminalScore < 64) {
			GPA = 1.0f;
		} else {
			GPA = 0;
		}
		return GPA;
	}

	@Override
	public Score calAll(String studentNo) throws ServiceException {
		Score score = null;
		try {
			score = new Score();
			score  =studentCDao.calAll(studentNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return score;
	}

	@Override
	public boolean checkStudentCExistById(String teacherNo, String courseNo ,Integer tcTeacherNo)
			throws ServiceException {
		boolean flag = true;
		try {
			flag  =studentCDao.checkStudentCExistById(teacherNo, courseNo, tcTeacherNo);
		} catch (DaoException e) {
			e.printStackTrace();
			throw new DaoException(e.getMessage(), e);
		}
		return flag;
	}


}
